package com.px.lovepath.ui.home;

import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.px.lovepath.R;
import com.px.lovepath.ui.general.ViewController;

public class MenuController extends ViewController implements OnTouchListener {

	private boolean mMenuShowing;
	private final RelativeLayout mComposerButtonsWrapper;
	private final ImageView mComposerButtonsShowHideButtonPlus;
	private final RelativeLayout mComposerButtonsShowHideButton;
	private MenuChangedListener mListener;
	
	public MenuController(Activity parentActivity, View view, MenuChangedListener listener) {
		super(parentActivity, view);
		mListener = listener;
		mComposerButtonsWrapper = (RelativeLayout) view.findViewById(R.id.home__home_menu_view__composer);
		mComposerButtonsShowHideButton = (RelativeLayout) view.findViewById(R.id.home__home_menu_view__hide);
		mComposerButtonsShowHideButtonPlus = (ImageView) view.findViewById(R.id.home__home_menu_view__plus);

		mComposerButtonsShowHideButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onClickView(v, false);
			}
		});
		for (int i = 0; i < mComposerButtonsWrapper.getChildCount(); i++) {
			mComposerButtonsWrapper.getChildAt(i).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					mListener.onMenuChanged(arg0.getId());
//					/Toast.makeText(getParentActivity(), "argo=" + arg0.getId() + " click", 300).show();
				}
			});
		}

		mComposerButtonsShowHideButton.startAnimation(MenuAnimations.getRotateAnimation(0, 360, 200));

		View v = view.findViewById(R.id.home__home_menu_view__composer);
		v.setOnTouchListener(this);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		onClickView(v, true);
		return false;
	}

	public void onClickView(View v, boolean isOnlyClose) {
		if (isOnlyClose) {
			if (mMenuShowing) {
				MenuAnimations.startAnimationsOut(mComposerButtonsWrapper, 300);
				mComposerButtonsShowHideButtonPlus.startAnimation(MenuAnimations.getRotateAnimation(-315, 0, 300));
				mMenuShowing = !mMenuShowing;
			}
		} else {
			if (!mMenuShowing) {
				MenuAnimations.startAnimationsIn(mComposerButtonsWrapper, 300);
				mComposerButtonsShowHideButtonPlus.startAnimation(MenuAnimations.getRotateAnimation(0, -315, 300));
			} else {
				MenuAnimations.startAnimationsOut(mComposerButtonsWrapper, 300);
				mComposerButtonsShowHideButtonPlus.startAnimation(MenuAnimations.getRotateAnimation(-315, 0, 300));
			}
			mMenuShowing = !mMenuShowing;
		}
	}
	
	// ### 内部类 ###
	public interface MenuChangedListener {
		void onMenuChanged(int selected);
	}

}
